import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Administrator on 2017/5/5.
 */
public class EMDServer {
    public static void main(String[] args) {
        try {
            String PROTOCOL = "C200";
            String TEMPCODE = "5601";
            String TRANSBRANCH = "9909";
            String SRCSYSTEM = "56";
            String ADDRESS = "18650495842";
            String TIMESTAMP = "20161206094711";
            String MSG = "这是你的内容";
            String _EmdUtil = "com.csii.great.end.data.EmdUtil@dfkdsjfs";
            String MSGCOUNT = "1";
            String RESULTINFO = "交易成功";
            StringBuffer sbServer = new StringBuffer();
            sbServer.append("<?xml version=\"1.0\" encoding=\"gb2312\" ?>");
            sbServer.append("<EMD>");
            sbServer.append("<HEAD>");
            sbServer.append("<PROTOCOL>");
            sbServer.append(PROTOCOL);
            sbServer.append("</PROTOCOL>");
            sbServer.append("<TEMPCODE>");
            sbServer.append(TEMPCODE);
            sbServer.append("</TEMPCODE>");
            sbServer.append("<TRANSID></TRANSID>");
            sbServer.append("<TRANSCOMMAND></TRANSCOMMAND>");
            sbServer.append(TRANSBRANCH);
            sbServer.append("<TRANSBRANCH>");

            sbServer.append("</TRANSBRANCH>");
            sbServer.append("<SRCSYSTEM>");
            sbServer.append(SRCSYSTEM);
            sbServer.append("</SRCSYSTEM>");
            sbServer.append("<SRCDEVICE></SRCDEVICE>");
            sbServer.append("<TIMESTAMP>");
            sbServer.append(TIMESTAMP);
            sbServer.append("</TIMESTAMP>");
            sbServer.append("</HEAD>");
            sbServer.append("<MSGCOUNT>");
            sbServer.append(MSGCOUNT);
            sbServer.append("</MSGCOUNT>");
            sbServer.append("<TIMESTAMP>");
            sbServer.append(TIMESTAMP);
            sbServer.append("</TIMESTAMP>");
            sbServer.append("<PROTOCOL>");
            sbServer.append(PROTOCOL);
            sbServer.append("</PROTOCOL>");
            sbServer.append("<RESULTINFO>");
            sbServer.append(RESULTINFO);
            sbServer.append("</RESULTINFO>");

            sbServer.append("<BODY>");
            sbServer.append("<MSG>" + MSG + "</MSG>");
            sbServer.append("<ADDRESS>" + ADDRESS + "</ADDRESS>");
            sbServer.append("<_EmdUtil>" + _EmdUtil + "</ADDRESS>");
            sbServer.append("</BODY>");
            sbServer.append("</EMD>");
            //1.建立一个服务器Socket(ServerSocket)绑定指定端口
            ServerSocket serverSocket = new ServerSocket(8800);
            //2.使用accept()方法阻止等待监听，获得新连接
                 Socket socket = serverSocket.accept();
                //3.获得输入流
                InputStream is = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                //获得输出流
                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);
                //4.读取用户输入信息
                String sbClient = null;
                while (!((sbClient = br.readLine()) == null)) {
                    System.out.println("我是服务器，用户报文：" + sbClient);
                }
                //给客户一个响应
                pw.write(sbServer.toString());
                pw.flush();
                //5.关闭资源
                pw.close();
                os.close();
                br.close();
                is.close();
                socket.close();
                serverSocket.close();


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public class ServerSocketThread implements Runnable {

        @Override
        public void run() {

        }
    }
}
